/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package embeddedpaint.user_interface.tools.dialog;

import embeddedpaint.model.layers.LayerManager;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class FrameUpdateProperties extends JFrame
{
    private LayerManager        m_layerManager;
    private JFrame              m_application;
    
    private JSpinner            m_txtWidth              = new JSpinner();
    private JSpinner            m_txtHeight             = new JSpinner();
    private JButton             m_btColorVisualizer     = new JButton();
    private JCheckBox           m_ckUseTransparent      = new JCheckBox();

    public FrameUpdateProperties (LayerManager layerManager, JFrame mainFrame)
    {
        this.m_layerManager  = layerManager;
        this.m_application   = mainFrame;
        this.m_application.setEnabled(false);
        
        this.setName("Upadate main propreties");
        this.setLocation(this.m_application.getX() + 100, this.m_application.getY() + 100);
        this.setVisible(true);
        this.setEnabled(true);
        this.setAlwaysOnTop(true);
        this.setAutoRequestFocus(true);
        this.setResizable(false);
        this.setPreferredSize(new Dimension(200, 150));

        this.initComponent();
        this.initValue();
        
        this.pack();
    }

    private void initValue ()
    {
        this.m_txtHeight.setModel(new SpinnerNumberModel(this.m_layerManager.getM_height(), 1, Integer.MAX_VALUE, 1));
        this.m_txtWidth .setModel(new SpinnerNumberModel(this.m_layerManager.getM_width() , 1, Integer.MAX_VALUE, 1));
        
        if(this.m_layerManager.getM_backroundColor() != null)
        {
            this.m_btColorVisualizer.setBackground(this.m_layerManager.getM_backroundColor());
            this.m_ckUseTransparent.setSelected(false);
        }
        else
        {
            this.m_ckUseTransparent.setSelected(true);
        }
        this.checkNoColor();
    }
    
    private void close ()
    {
        this.m_application.setEnabled(true);
        this.dispose();
    }
    
    private void update ()
    {
        this.m_layerManager.setM_height((int) this.m_txtHeight.getModel().getValue());
        this.m_layerManager.setM_width ((int) this.m_txtWidth .getModel().getValue());
        
        if(this.m_ckUseTransparent.isSelected())
        {
            this.m_layerManager.setM_backroundColor(null);
        }
        else
        {
            this.m_layerManager.setM_backroundColor(this.m_btColorVisualizer.getBackground());
        }
        this.close();
    }
    
    private void selectColor ()
    {
        final JColorChooser colorChooser = new JColorChooser();
        
        this.setAlwaysOnTop(false);
        
        Color newColor = colorChooser.showDialog(this.m_application, "color Chooser",  m_btColorVisualizer.getBackground());
        
        this.m_btColorVisualizer.setBackground(newColor);
        this.m_btColorVisualizer.repaint();
    }
    
    private void checkNoColor ()
    {
        if(this.m_ckUseTransparent.isSelected())
        {
            this.m_btColorVisualizer.setEnabled(false);
        }
        else
        {
            this.m_btColorVisualizer.setEnabled(true);
        }
    }

    private void initComponent ()
    {
        JButton buttonOK = new JButton("Validate");
        JButton buttonCancel = new JButton("Cancel");
        
        this.setLayout(new GridLayout(6, 2));
        
        this.add(new JLabel("Width :"));
        this.add(this.m_txtWidth);
        
        this.add(new JLabel("Height :"));
        this.add(this.m_txtHeight);
        
        this.add(new JLabel("Backround :"));
        this.add(this.m_btColorVisualizer);
        
        this.add(new JLabel("Transparent :"));
        this.add(this.m_ckUseTransparent);
        
        this.add(new JLabel());
        this.add(new JLabel());
        
        this.add(buttonOK);
        this.add(buttonCancel);
       
        this.m_ckUseTransparent.addChangeListener(new ChangeListener() 
        {
            @Override
            public void stateChanged (ChangeEvent e)
            {
                checkNoColor();
            }
        });
        
        this.m_btColorVisualizer.addActionListener(new ActionListener() 
        {
            @Override
            public void actionPerformed (ActionEvent e)
            {
                selectColor();
            }
        });
        
        buttonCancel.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed (ActionEvent e)
            {
                close();
            }
        });
        buttonOK.addActionListener(new ActionListener() 
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                update();
            }
        });
        
        this.addWindowListener(new WindowListener() 
        {
            @Override
            public void windowOpened(WindowEvent e)
            {
            }

            @Override
            public void windowClosing(WindowEvent e)
            {
                close();
            }

            @Override
            public void windowClosed(WindowEvent e)
            {
            }

            @Override
            public void windowIconified(WindowEvent e)
            {
            }

            @Override
            public void windowDeiconified(WindowEvent e)
            {
            }

            @Override
            public void windowActivated(WindowEvent e)
            {
            }

            @Override
            public void windowDeactivated(WindowEvent e)
            {
            }
        });
    }
}
